﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class ApplicationRepository : BaseRepository
    {

        public int AddApplication(string applicationName, int enterpriseId=1)
        {
            int applicationId = -1;
            try
            {
                Application application = new Application();
                application.Name = applicationName;
                application.EnterpriseId = enterpriseId;
                application.adt_DateCreated = DateTime.Now;
                _context.Applications.Add(application);
                _context.SaveChanges();
                applicationId = application.ApplicationId;
            }
            catch (Exception ex)
            {
                applicationId = -1;
            }

            return applicationId;
        }

        public bool RemoveApplication(string applicationName, int enterpriseId = 1)
        {
            bool bResult = true;

            try
            {
                Application application = _context.Applications.Where(p => p.Name == applicationName && p.EnterpriseId == enterpriseId).SingleOrDefault();
                if (null != application)
                {
                    _context.Applications.Remove(application);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public bool RemoveApplication(int appId, int enterpriseId = 1)
        {
            bool bResult = true;

            try
            {
                Application application = _context.Applications.Where(p => p.ApplicationId == appId && p.EnterpriseId == enterpriseId).SingleOrDefault();
                if (null != application)
                {
                    _context.Applications.Remove(application);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public Application GetApplicationByName(string applicationName, int enterpriseId = 1)
        {
            Application application = _context.Applications.Where(p => p.Name == applicationName && p.EnterpriseId == enterpriseId).SingleOrDefault();
            return application;
        }

        public SecurityApi.Model.Application GetApplicationById(int id)
        {
            SecurityApi.Model.Application app = null;
            Application e = _context.Applications.Where(p => p.ApplicationId == id).SingleOrDefault();

            if (null != e)
            {
                app = new Model.Application { ApplicationId = e.ApplicationId, Name = e.Name, Title = e.Title, Active = e.Active, Area = e.Area, DefaltAction = e.DefaltAction, DefaltController = e.DefaltController };
            }
            return app;
        }

        public IQueryable<SecurityApi.Model.Application> GetAll()
        {
            var query = from a in _context.Applications
                        join e in _context.Enterprises on a.EnterpriseId equals e.EnterpriseId
                        select new SecurityApi.Model.Application
                                {
                                    ApplicationId = a.ApplicationId,
                                    Name = a.Name,
                                    Title = a.Title,
                                    Active = a.Active,
                                    Area = a.Area,
                                    DefaltAction = a.DefaltAction,
                                    DefaltController = a.DefaltController,
                                    DisplayOrder = a.DisplayOrder,
                                    Enterprise = new Model.Enterprise { Id = e.EnterpriseId, Name = e.Name,Title=e.Title,Active=e.Active }
                                };
            query = query.OrderBy(p => p.DisplayOrder);

            return query;
        }

    }
}
